package com.example.shop.common.api;

import com.fasterxml.jackson.annotation.JsonIgnore;

import java.util.List;

/**
 * 通用返回对象
 */
public class ResponseResult<T> {
    private long code;
    private String message;
    private T data;
    private static final String RESOURCES_ROOT_DIR = "D://electricity_shop//resources//";
    private static final String RESOURCES_SERVER_IP = "http://127.0.0.1:8080/";

    public static String toRequestPath(String resources){
        if(!resources.contains(RESOURCES_ROOT_DIR)){
            if(resources.contains(RESOURCES_SERVER_IP)){
                return resources;
            }
            try {
                throw new Exception("资源目录有问题");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        int requestPathIndex = resources.indexOf(RESOURCES_ROOT_DIR);
        String requestPath = resources.substring(requestPathIndex+RESOURCES_ROOT_DIR.length());
        return RESOURCES_SERVER_IP+requestPath;
    }

    protected ResponseResult() {
    }
    public ResponseResult(StateCode code){
        this.code = code.getCode();
        this.message = code.getMessage();
        this.data = null;
    }
    protected ResponseResult(StateCode code, T data) {
        this.code = code.getCode();
        this.message = code.getMessage();
        this.data = data;
    }




    /**
     * 业务成功处理，则响应回一个结果对象
     * @param data 响应体的数据
     * @return 返回一个响应结果对象
     */
    public static <T> ResponseResult<T> success(T data){
        return new ResponseResult<T>(ServiceStateCode.SUCCESS,data);
    }
    /**
     * 业务失败处理，则响应回一个结果对象
     * @param code 状态码
     * @return 返回一个响应结果对象
     */
    public static <T> ResponseResult<T> failure(StateCode code){
        return new ResponseResult<>(code);
    }

    public static <T> ResponseResult<T> auto(List<StateCode> code, T data){
        for(StateCode state:code){
            if(!ServiceStateCode.SUCCESS.equals(state)){
                if(data instanceof Boolean){
                    return (ResponseResult<T>) booleanResp(state, (Boolean) data);
                }
                return failure(state);
            }
        }
        return success(data);
    }

    public static <T> ResponseResult<T> auto(StateCode code,T data){
        if(ServiceStateCode.SUCCESS.equals(code)){
            return success(data);
        }
        if(data instanceof Boolean){
            return (ResponseResult<T>) booleanResp(code, (Boolean) data);
        }
        return failure(code);
    }
    public static ResponseResult<Boolean> booleanResp(StateCode code, Boolean data){
        return new ResponseResult<>(code,data);
    }


    public long getCode() {
        return code;
    }

    public void setCode(long code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
